﻿import Vue from 'vue'
import Router from 'vue-router'
import HelloWorld from '@/components/HelloWorld'
// 登陆、注册的界面路由
import login from '../pages/login/login.vue'
import register from '../pages/login/register.vue'
import mianLogin from '../pages/login/mainLogin.vue'

import app from '../App.vue'
// 主界面路由
import mainPage from '../pages/main/mainPage.vue'
import queryPost from '../pages/main/queryPost.vue'
import queryForum from '../pages/main/queryForum.vue'
import sendPost from '../pages/main/sendPost'
import comment from '../pages/main/comment.vue'
// 用户界面
import user from '../pages/user/user.vue'
import userInfo from '../pages/user/userInfo.vue'
import userPost from '../pages/user/userPost.vue'
import userComment from '../pages/user/userComment.vue'
import apply from '../pages/user/apply.vue'

// 版主界面
import manageForum from '../pages/user/manageForum.vue'

// 管理员界面
import mUser from '../pages/admin/manageUser.vue'
import mUser_i from '../pages/admin/user_info.vue'
import mUser_a from '../pages/admin/user_apply.vue'
import mPost from '../pages/admin/managePost.vue'
import mForum from '../pages/admin/manageForum.vue'
import mOther from '../pages/admin/manageOther.vue'

// test
import test from '../test.vue'

Vue.use(Router)

const router = new Router({
  routes: [{
      path: '/',
      name: 'index',
      redirect: '/login'
    },
    {
      path: '/login',
      component: mianLogin,
      children: [{
          path: '',
          redirect: '/login/login',
          component: login
        },
        {
          path: 'login',
          name: 'login',
          component: login
        },
        {
          path: 'register',
          name: 'register',
          component: register
        }
      ]
    },
    {
      path: '/main',
      name: 'main',
      component: mainPage,
      children: [{
          path: 'queryPost',
          name: 'queryPost',
          component: queryPost
        },
        {
          path: 'sendPost',
          name: 'sendPost',
          component: sendPost
        }, {
          path: 'comment',
          name: 'comment',
          component: comment
        }
      ]
    },
    {
      path: '/queryForum',
      name: 'queryForum',
      component: queryForum
    },
    {
      path: '/user',
      component: user,
      children: [{
          path: '',
          redirect: '/user/userInfo',
          component: userInfo
        },
        {
          path: 'userInfo',
          name: 'userInfo',
          component: userInfo

        }, {
          path: 'userPost',
          name: 'userPost',
          component: userPost
        }, {
          path: 'userComment',
          name: 'userComment',
          component: userComment
        }, {
          path: 'manageForum',
          name: 'manageForum',
          component: manageForum
        }, {
          path: 'apply',
          name: 'apply',
          component: apply
        }
      ]
    },
    {
    	path: '/manageUser',
    	component: mUser,
    	children:[{
    		path:'',
    		redirect:'/manageUser/user_info' ,
    		component:mUser_i
    	},{
    		path:'user_info',
    		name:'user_Info' ,
    		component:mUser_i
    	},{
    		path:'user_apply',
    		name:'user_apply' ,
    		component:mUser_a
    	}
    	]
    },
    {
    	path: '/managePost',
    	component: mPost
    },
    {
    	path: '/manageForum',
    	component: mForum
    },
    {
    	path: '/manageOther',
    	component: mOther
    },
    {
      path: '/test',
      component: test
    }


  ]
})

router.beforeEach((to, from, next) => {
  const nextRoute = ['sendPost', 'user'];
  let isLogin = localStorage.getItem('user');

  //if(nextRoute.indexOf(to.name) >= 0) {
  //  if(isLogin === null) {
  //    alert("请登录！");
  //    router.push({
  //      name: 'login'
  //    });
  //    console.log(1)
  //
  //  }
  //}
  if(to.path === '/login') {
    if(isLogin != null) {
      next({
        name: 'queryForum'
      })
    }
  }

  next();
})

export default router;